

import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.Enumeration;
import javax.swing.*;

import Client.GameClient;
import Server.GameServer;

import java.util.List;
import java.util.LinkedList;


public class OuterShell  extends JFrame{



	/**
	 * 
	 */
	private static final long serialVersionUID = 4591201635872889397L;
	private static final String GAME_BIND_NAME = "PirateGame";

	private static GameServer mGameServer;
	
	/**
	 * Create the registory for the 
	 */
	private static void createRegistory(){

		try{

			java.rmi.registry.LocateRegistry.createRegistry(1099);

		} catch(ExportException eE){

			System.out.println("Registory Already there");

		} catch (RemoteException e) {
			System.out.println("Registory Already there");
		}

	}

	private static List<InetAddress> findOtherComputers() throws Exception{

		List<InetAddress> otherComputerOnTheNetWork = new LinkedList<InetAddress>();
		Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();

		while(e.hasMoreElements()) {
			NetworkInterface ni = e.nextElement();
			System.out.println("Net interface: "+ni.getName());

			Enumeration<?> e2 = ni.getInetAddresses();

			while (e2.hasMoreElements()){
				InetAddress ip = (InetAddress) e2.nextElement();
				System.out.println("IP address: "+ ip.toString());
				otherComputerOnTheNetWork.add(ip);
			}
		}

		return otherComputerOnTheNetWork;

	}

	@SuppressWarnings("unchecked")
	private static void findServerOnComputers(List<InetAddress> computersOnTheNetWork){



		findServerLoop : for(InetAddress a :  computersOnTheNetWork ){

			try{
//				myShips =  (RemoteList<PirateShip>) Naming.lookup("rmi://" + a.getHostAddress() + "/" + SHIPS_BIND_NAME);
//				myBalls =  (RemoteList<CannonBall>) Naming.lookup("rmi://" + a.getHostAddress() + "/" + BALLS_BIND_NAME);
//				myWind =  (RemoteList<Wind>) Naming.lookup("rmi://" + a.getHostAddress() + "/" + WIND_BIND_NAME);
				mGameServer = (GameServer) Naming.lookup("rmi://" + a.getHostAddress() + "/" + GAME_BIND_NAME);
				System.out.println("Success");

				break findServerLoop;

			} catch(Exception e){


				//because the game could not connect to the server then we make should we are not ref
				// referencing things that we are not supposed to.
//				myShips =  null;
//				myBalls =  null;
//				myWind =  null;

				//prints out the game could not connect to the I.P. address
				System.out.println("Failed to find server on: " + a.toString());

			}
		}




	}

	private static GameServer createServer() throws RemoteException, MalformedURLException, Exception{

		GameServer pirateGame = new GameServer();
		new Thread( pirateGame).start();
		
		return pirateGame;
		
	}

	public static void main(String [] args){


		try{

			createRegistory();
			List<InetAddress> computersOnTheNetWork = findOtherComputers();
			findServerOnComputers(computersOnTheNetWork);
			
			// checks to see if you have connected to the gameS
			if(mGameServer == null){
				
				System.out.println("Server out!");
				mGameServer = createServer(); // If there are reachable servers on the net create a new one

			}
			
			//Creates a new client in another thread.
			Runnable r = new Runnable() {
				public void run() {
					try {
						new GameClient(mGameServer);
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			};
			SwingUtilities.invokeLater(r);



		} catch(Exception ex){

			ex.printStackTrace();

		}
	}

}
